package com.android.love.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.app.Dialog;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.drm.DrmStore.ConstraintsColumns;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;

import com.android.love.MyApplication;
import com.android.love.util.ActivityUtil;
import com.android.love.util.Constant;
import com.github.kevinsawicki.wishlist.ViewFinder;

public class BaseActivity extends FragmentActivity implements BetterActivity, Constant {

	private boolean wasCreated, wasInterrupted;

	private int progressDialogTitleId;

	private int progressDialogMsgId;

	private Intent currentIntent;

    /** 绑定这个activity布局的Finder **/
    protected ViewFinder finder;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.wasCreated = true;
		this.currentIntent = getIntent();

		Application application = getApplication();
		if (application instanceof MyApplication) {
			((MyApplication) application).setActiveContext(getClass()
					.getCanonicalName(), this);
		}
        finder = new ViewFinder(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		((MyApplication) getApplication()).resetActiveContext(getClass()
				.getCanonicalName());
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		wasInterrupted = true;
	}

	@Override
	protected void onPause() {
		super.onPause();
		wasCreated = wasInterrupted = false;
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		this.currentIntent = intent;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		return BetterActivityHelper.createProgressDialog(this,
				progressDialogTitleId, progressDialogMsgId);
	}

	public void setProgressDialogTitleId(int progressDialogTitleId) {
		this.progressDialogTitleId = progressDialogTitleId;
	}

	public void setProgressDialogMsgId(int progressDialogMsgId) {
		this.progressDialogMsgId = progressDialogMsgId;
	}

	public boolean isRestoring() {
		return wasInterrupted;
	}

	public boolean isResuming() {
		return !wasCreated;
	}

	public boolean isLaunching() {
		return !wasInterrupted && wasCreated;
	}

	public boolean isApplicationBroughtToBackground() {
		return BetterActivityHelper.isApplicationBroughtToBackground(this);
	}

	public Intent getCurrentIntent() {
		return currentIntent;
	}

	public boolean isLandscapeMode() {
		return getWindowManager().getDefaultDisplay().getOrientation() == 1;
	}

	public boolean isPortraitMode() {
		return !isLandscapeMode();
	}

	public AlertDialog newYesNoDialog(int titleResourceId,
			int messageResourceId, OnClickListener listener) {
		return BetterActivityHelper.newYesNoDialog(this,
				getString(titleResourceId), getString(messageResourceId),
				android.R.drawable.ic_dialog_info, listener);
	}

	public AlertDialog newInfoDialog(int titleResourceId, int messageResourceId) {
		return BetterActivityHelper.newMessageDialog(this,
				getString(titleResourceId), getString(messageResourceId),
				android.R.drawable.ic_dialog_info);
	}

	public AlertDialog newAlertDialog(int titleResourceId, int messageResourceId) {
		return BetterActivityHelper.newMessageDialog(this,
				getString(titleResourceId), getString(messageResourceId),
				android.R.drawable.ic_dialog_alert);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		BetterActivityHelper.handleApplicationClosing(this, keyCode);
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public int getWindowFeatures() {
		return BetterActivityHelper.getWindowFeatures(this);
	}

	@Override
	public Dialog newCustomDialog(int theme, View view) {
		// TODO Auto-generated method stub
		return BetterActivityHelper.newCustomDialog(this, theme, view);
	}

	/**
	 * 带动画的finish事件
	 */
	public void finish(boolean isEnd) {
		super.finish();
		ActivityUtil.runActivityAnim(this, isEnd);
	}

	/**
	 * 一般finish事件
	 */
	public void defaultFinish() {
		super.finish();
	}

}
